﻿

$(document).ready(function () {
    //初始化表单验证
    initValidate();

    if ($("#Id").val() > 0) {
        $("#Username").attr("disabled", true);
    }
   
    //保存
    $("#btnsave").click(function (evt) { evt.preventDefault(); save($(this)); });
    //取消
    $("#btncancel").click(function (evt) { parent.closeDialog(); return false; });
    //全选
    $(".checkAll").click(function (evt) {
        $("#list_table [type='checkbox']").prop("checked", true);
        $("#list_table [type='checkbox']").change();
    });
    //清空
    $(".clearAll").click(function (evt) {
        $("#list_table [type='checkbox']").prop("checked", false);
        $("#list_table [type='checkbox']").change();
    });
    //选中角色是修改行颜色
    $("#list_table [type='checkbox']").change(function (evt) {
        if ($(this).prop("checked"))
            $(this).closest("tr").addClass("list-row-selected");
        else
            $(this).closest("tr").removeClass("list-row-selected");
    });
    $("#list_table [type='checkbox']").change();
});

//初始化表单验证
function initValidate() {
    //表单验证
    $('#inputform').validate({
        rules: {
            Username: {
                required: true,
                //username: true,
                maxlength: 20, minlength: 2,
                remote: {
                    url: baseurl + 'Manage/User/ExistCheck',
                    type: 'post',
                    data: {
                        id: $("#Id").val(),
                        menuid: $("#Username").val()
                    },
                    dataFilter: function (data, type) {
                        if (data == "yes")
                            return true;
                        else
                            return false;
                    }
                }
            },
            Realname: {
                required: true, maxlength: 20, minlength: 2
            },
            //Dept_id: {
            //    required: true
            //},
            SiteId: {
                required: true
            },
            Tel: {
                isPhone: true
            },
            Mobile: {
                isMobile: true
            },
            Email: {
                required: true, email: true, maxlength: 100
            },
            Remark: {
                maxlength: 100
            },
            Password: {
                required: true,
                //remote: {
                //    url: baseurl + 'Manage/User/PwdLengthChk',
                //    type: 'post',
                //    data: {
                //        password: $("#Password").val()
                //    },
                //    dataFilter: function (data, type) {
                //        if (data == "yes")
                //            return true;
                //        else
                //            return false;
                //    }
                //}
            },
            ConfrimPassword:
                {
                    equalTo: "#Password"
                }           
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent());
        },
        messages: {
            Username: {
                remote: '用户名已被占用'
            },
            Password: {
                remote: '您的密码必须是6~16位的字符，且必须是数字和字母的组合。可以使用特殊符号，字母区分大小写！'
            },
            ConfrimPassword: {
                equalTo: '两次密码输入不一致'
            }
        }
    });
}
$("#Dept_id").change(function () {
    wareHouse();
});

function wareHouse() {
    var Dept_id = $("#Dept_id").val();
    $.ajax({
        type: "post",
        url: baseurl + "Manage/User/LanguageSelect",
        data: { Dept_id: Dept_id },
        success: function (data, status) {
            $("#Language").val(data);
        }
       
    });
}

//保存数据
function save(obj) {
   // debugger;
    if (!$("#inputform").valid()) {
        return;
    }

    var jsondata = {
        User: {
            Id: $("#Id").val(),
            Values: {}
        },
        UserRoles: []
    };
    ////记录ID
    //jsondata.Id = $("#Id").val();
    //主表数据
    $("#inputform .datafield").each(function () {
        jsondata.User.Values[$(this).attr("data-fieldname")] = $(this).val();
    });
    jsondata.User.Values["Password"] = $("[data-fieldname='Password']").val();
    jsondata.User.Values["Islocked"] = $("[data-fieldname='Islocked']").prop("checked") ? "1" : "0";
    //获取选中的用户角色
    $("#list_table .list-data tr").each(function () {
        var rowdata = {
            Roleid: $(this).find("#Roleid").val(),
            Ischecked: $(this).find("[type='checkbox']").prop("checked")
        };

        jsondata.UserRoles.push(rowdata);
    });

    //禁用保存按钮，防止重复提交
    $(obj).attr("disabled", true);

    $.ajax({
        type: "post",
        url: baseurl + "Manage/User/Save",
        data: {
            json: JSON.stringify(jsondata)
        },
        success: function (e) {
            $(obj).attr("disabled", false);
            if (e.Ret == "0") {
                parent.closeDialog(true);
            } else {
                $.messager.alert(csms_lang.message_failure, e.Msg, "error");
            }
        },
        error: function (xhr) {
            $(obj).attr("disabled", false);

            showError(xhr);
        }
    });
}